package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.PermissionService;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RequestMapping("/role")
@RestController

public class RoleController{
    @Reference
    private RoleService roleService;
    @Reference
    private PermissionService permissionService;
    @Reference
    private MenuService menuService;

    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[角色-分页查询]", queryPageBean);
        //rpc查询结果
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        //构造返回对象
        return new Result(true, "查询角色信息成功!", pageResult);
    }

    @GetMapping("/findAll")
    public Result findAll() {
        log.info("[权限-信息查询]data:{}");
        Map<String, Object> map = new HashMap<>();
        List<Permission> permission1 = permissionService.fandAll();
        List<Menu> menus = menuService.findAll();
        map.put("permission1", permission1);
        map.put("menus", menus);
        return new Result(true, "查询信息成功!", map);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    public Result add(@RequestBody Role role, Integer[] PermissionIds, Integer[] MenuIds) {
        log.info("[角色-添加]data:{},permissions,menus", role, PermissionIds, MenuIds);
        //rpc添加数据
        roleService.add(role, PermissionIds, MenuIds);
        return new Result(true, "添加角色信息成功!");
    }

    @GetMapping("/findById")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findById(Integer id) {
        log.info("[编辑-查询]id:{}", id);
        Role role = roleService.findById(id);
        return new Result(true,"编辑角色查询成功!",role);

    }
    @GetMapping("/RoleAndPermission")
    public Result RoleAndPermission(Integer id){
        List<Integer> list = roleService.RoleAndPermission(id);
        return new Result(true,"编辑权限查询成功!",list);
    }
    @GetMapping("/RoleAndMenu")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result RoleAndMenu(Integer id){
        List<Integer> list = roleService.RoleAndMenu(id);
        return new Result(true,"编辑菜单查询成功!",list);
    }
    @PostMapping("edit")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result edit(@RequestBody Role role, Integer[] PermissionIds, Integer[] MenuIds){
        log.info("[角色-更新]data:{},PermissionIds,menus", role, PermissionIds, MenuIds);
        roleService.update(role,PermissionIds,MenuIds);
        return new Result(true,"编辑更新成功!");
    }
    @PostMapping("delete")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result delete(Integer id){
        log.info("[角色-删除]data:{}",id);
        roleService.delete(id);
        return new Result(true,"角色删除");
    }
}
